var Cat = require('../models/cat.js');
// var Course = require('../model/course.js');

exports.add = function (req, res) {
	let _category = req.body;
	console.log(_category);
	let category = new Cat(_category);
	category.save(function (err, category) {
		if (err) {
			return res.status(403).json({msg: '添加失败，请重试', err});
		}
		res.json({
			msg: '分类添加成功',
			category
		})
	})
}

exports.list = function (req, res) {
	const fields = ['name', 'createdAt', 'updatedAt', 'courses'];
	Cat.find({}, fields, function(err, cats) {
		if (err) {
			return res.status(500).json({msg: '获取分类失败，请重试'});
		}
		res.json({
			msg: '获取分类成功',
			cats
		});
	})
}

exports.del = function (req, res) {
	var id = req.query.id;
	if (id) {
		Cat.remove({_id: id}, function (err, category) {
			if (err) {
				return res.status(400).json({msg: '删除失败， 请重试'});
			}
			res.json({
				msg: '删除成功'
			})
		})
	} else {
		res.status(400).json({msg: '请求失败'});
	}
}
